package com.chenchao.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/26 10:21
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.net.Test11B.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * TCP服务端
 */
public class Test11B {
    public static void main(String[] args) throws IOException {
        /*新建ServerSocket对象*/
        ServerSocket serverSocket = new ServerSocket(10000);
        /*接收socket，接收不到客户会死等（阻塞）*/
        Socket accept = serverSocket.accept();

        /*输入流读出*/
        BufferedReader br = new BufferedReader(new InputStreamReader(accept.getInputStream()));
        String str;
        while((str=br.readLine())!=null){
            System.out.println(str);
        }
        /*输出流写入*/
        OutputStream os = accept.getOutputStream();
        os.write("我收到了消息，\ncopy".getBytes());

        /*关闭资源*/
        br.close();
        os.close();
        accept.close();
        serverSocket.close();
    }
}
